Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relations: Add functionality to sort json refs by relation_type #1202

Conversation

sakshamarora1
Copy link
Contributor

❤️ Thank you for your contribution!

Description

Closes: CERNDocumentServer/cds-ils#827

Copy link
Contributor

@jrcastro2 jrcastro2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a couple of questions as it looks a bit complex 😅

@@ -26,7 +26,7 @@ def jsonresolver_loader(url_map):
def relations_resolver(document_pid):
"""Resolve record relations and add metadata."""
document = Document.get_record_by_pid(document_pid)
return document.relations
return document.relations(sort_by = ["edition"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: I guess that sort_by is a list because we want to be able to define a different sort type for the different type of relations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, as relations have a separate object list in the response

@@ -170,6 +170,11 @@ def get(self):
r = self._build_relation_obj(sibling_pid, name)
relations.setdefault(name, [])
relations[name].append(r)

# Pre-requisite: fields being sorted should be of the same type.
if sort_by and name in sort_by and name in relations.keys():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Does this mean that the EDITION_RELATION can only be sorted by edition, LANGUAGE_RELATION only by language and OTHER_RELATION by other?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes!
Do we want the ability to sort by another field apart from the relation type?

@@ -421,12 +421,10 @@ def remove(self, previous_rec, next_rec, relation_type):
class IlsRecordWithRelations(IlsRecord):
"""Add relations functionalities to records."""

@property
def relations(self):
def relations(self, sort_by=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a breaking change in the class interface, did you test all the possible occurences?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in addition: I think the sort should be somewhat defined per relation type (maybe config of relation types?) not as an argument of the function - we risk the list of arguments growing infinitely

@@ -421,12 +421,10 @@ def remove(self, previous_rec, next_rec, relation_type):
class IlsRecordWithRelations(IlsRecord):
"""Add relations functionalities to records."""

@property
def relations(self):
def relations(self, sort_by=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could go for kwargs instead:

Suggested change
def relations(self, sort_by=None):
def relations(self, **kwargs):

@@ -224,14 +229,14 @@ def get(self):
return relations


def get_relations(record):
def get_relations(record, sort_by=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def get_relations(record, sort_by=None):
def get_relations(record, **kwargs):

and also inject kwargs in all the .get() methods


# Pre-requisite: fields being sorted should be of the same type.
if sort_by and name in sort_by and name in relations.keys():
relations[name].sort(key=lambda rec: rec["record_metadata"][name])
Copy link
Contributor

@ntarocco ntarocco Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might not good enough: we might be sorting too late, sorting the results of what was retrieved from the db, queried with a default sorting.

If this is what we want to do, then it might better to have the client, consuming the REST API response, to sort the results.
If instead we want to drive the sorting from the backend, then the sorting param should be applied to the db query.
However, this is very tricky, due to the nature of data stored as JSON blob.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed IRL, decision to be taken.

@@ -27,6 +27,7 @@
"invenio_app_ils.relations.nodes:PIDNodeRelated",
"invenio_pidrelations.serializers.schemas.RelationSchema",
"invenio_app_ils.relations.api.SiblingsRelation",
[],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[],
None,

Setting mutable parameters is in general considered as bad practice, since we can change it on a runtime - especially in this case it is configuration so we don't want to allow for changes

@kpsherva kpsherva merged commit 281494a into inveniosoftware:master Apr 4, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Edition not always in order in front office
4 participants